package com.supply.dl.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.supply.dl.R;
import com.supply.dl.utils.StringUtils;
import com.supply.dl.widget.fr.castorflex.android.circularprogressbar.CircularProgressBar;
import com.supply.dl.widget.span.JumpingBeans;

/**
 * <p>加载失败的组件图层</p>
 * <p>time 16:29</p>
 * @author antao
 * @version CmgeForum 1.0.0
 */
public class LoadTipFailedLayout extends FrameLayout
{
	/**
	 * 状态-加载中
	 */
	public static final int STATE_PROGRESS = 0;
	/**
	 * 状态-失败
	 */
	public static final int STATE_FAILED = 1;
	/**
	 * 状态-完成
	 */
	public static final int STATE_FINISH = 2;
	/**
	 * 状态-没有数据
	 */
	public static final int STATE_NORMAL = 3;
	/**
	 * 当前的状态
	 */
	private int state = STATE_PROGRESS;
	/**
	 * 进度圈圈
	 */
	private CircularProgressBar circularProgressBar;
	/**
	 * 加载失败的文字
	 */
	private TextView txtLoadFailed;
	/**
	 * 加载中的文字
	 */
	private TextView txtLoadTip;
	/**
	 * 失败/提示的文字
	 */
	private CharSequence strFailed, strNormal;

	private ImageView imgNormal;

	/**
	 * 加载的组件
	 */
	private View layoutLoad;
	/**
	 * 文字跳动的JumpingBeans
	 */
	private JumpingBeans jumpingBeans;

	public LoadTipFailedLayout(Context context)
	{
		super(context);
	}

	public LoadTipFailedLayout(Context context, AttributeSet attrs)
	{
		super(context, attrs);
	}

	public LoadTipFailedLayout(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
	}

	@Override
	protected void onFinishInflate()
	{
		super.onFinishInflate();
		layoutLoad = findViewById(R.id.layout_load);
		circularProgressBar = (CircularProgressBar) findViewById(R.id.progressbar_circular);
		txtLoadFailed = (TextView) findViewById(R.id.txt_load_failed);
		strFailed = txtLoadFailed.getText();
		strNormal = getContext().getResources().getString(R.string.since_help_list_not_has_data);
		imgNormal = (ImageView) findViewById(R.id.img_normal);
		txtLoadTip = (TextView) findViewById(R.id.txt_load_tip);

		setState(STATE_PROGRESS);
	}

	public int getState()
	{
		return state;
	}

	public void setState(int state)
	{
		this.state = state;
		switch (state)
		{
			case STATE_FAILED:
			{
				setVisibility(VISIBLE);
				txtLoadFailed.setVisibility(VISIBLE);
				layoutLoad.setVisibility(INVISIBLE);
				txtLoadFailed.setText(strFailed);
				imgNormal.setVisibility(GONE);
				if (jumpingBeans != null)
					jumpingBeans.stopJumping();
				break;
			}
			case STATE_FINISH:
			{
				txtLoadFailed.setVisibility(INVISIBLE);
				layoutLoad.setVisibility(INVISIBLE);
				setVisibility(GONE);
				imgNormal.setVisibility(GONE);
				if (jumpingBeans != null)
					jumpingBeans.stopJumping();
				break;
			}
			case STATE_PROGRESS:
			{
				setVisibility(VISIBLE);
				txtLoadFailed.setVisibility(INVISIBLE);
				layoutLoad.setVisibility(VISIBLE);
				imgNormal.setVisibility(GONE);
				break;
			}
			case STATE_NORMAL:
			{
				setVisibility(VISIBLE);
				layoutLoad.setVisibility(INVISIBLE);

				if (imgNormal.getDrawable() != null)
				{
					txtLoadFailed.setVisibility(GONE);
					txtLoadFailed.setText(strNormal);

					imgNormal.setVisibility(VISIBLE);
				}
				else
				{
					txtLoadFailed.setVisibility(VISIBLE);
					txtLoadFailed.setText(strNormal);

					imgNormal.setVisibility(GONE);
				}
				if (jumpingBeans != null)
					jumpingBeans.stopJumping();
				break;
			}
		}
	}

	public void setCpbColor(int color)
	{
		circularProgressBar.setCpbColor(color);
	}

	public void setFailedText(int rId)
	{
		strFailed = getContext().getString(rId);
	}

	public void setLoadText(int rId)
	{
		setLoadText(getContext().getString(rId));
	}

	public void setLoadText(String text)
	{
		txtLoadTip.setText(text);
		if (StringUtils.isEmpty(text))
		{
			if (jumpingBeans != null)
				jumpingBeans.stopJumping();
		}
		else
			jumpingBeans = new JumpingBeans.Builder().appendJumpingDots(txtLoadTip).build();
	}

	public void setNormalText(int rId)
	{
		strNormal = getContext().getString(rId);
	}

	public void setTextColor(int color)
	{
		txtLoadFailed.setTextColor(color);
		txtLoadTip.setTextColor(color);
	}

	/**
	 * 设置加载结束后的字体大小
	 * @param unit
	 * @param size
	 */
	public void setTextSize(int unit, int size){
		txtLoadFailed.setTextSize(unit, size);
	}

	public void setNorMalDrawable(int rId)
	{
		try
		{
			imgNormal.setImageResource(rId);
		}
		catch (Throwable throwable)
		{
			System.gc();
		}
	}

	public void setRestartOnClickListener(OnClickListener l)
	{
		imgNormal.setOnClickListener(l);
		txtLoadFailed.setOnClickListener(l);
	}

	public void setOnNormalClickListener(OnClickListener l)
	{
		imgNormal.setOnClickListener(l);
	}
}
